package com.study.serialization.demo.protobuf;

import com.google.protobuf.InvalidProtocolBufferException;

public class ProtobufTest {

    public static void main(String[] args) {
        //创建对象，及属性赋值
        entity.PersonProto.Person.Builder builder = entity.PersonProto.Person.newBuilder();

        builder.setName("Mrzhang").setAge(18);
        entity.PersonProto.Person person = builder.build();
        System.out.println(person);

        //序列化(通过protobuf生成的java类的内部方法进行序列化)
        byte[] bytes = person.toByteArray();

        //反序列化(通过protobuf生成的java类的内部方法进行反序列化)
        try {
            entity.PersonProto.Person parseFrom = entity.PersonProto.Person.parseFrom(bytes);
            System.out.println(parseFrom);
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
    }
}
